package com.ias.agdis_r.activity.more;

import java.io.File;

import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.ias.agdis_r.R;
import com.ias.agdis_r.activity.BaseKeyActivity;
import com.ias.agdis_r.activity.MainActivity;
import com.ias.agdis_r.manager.EffectManager;

/**
 * 图库-详情页面
 * @author zhangsc
 *
 */
public class PhotoDetailActivity extends BaseKeyActivity {

	private ImageView img_show;
	private ActionBar actionBar;
	Bitmap bitmap = null; 
	private AlertDialog dialog;
	private File file;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_photodetail);
		actionBar = getActionBar();
		loadactionbar();
		img_show = (ImageView)findViewById(R.id.img_show);

		Intent intent = getIntent();
		file = (File) intent.getExtras().get("photo");
		loadImage(file);
		
		img_show.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				if(file!=null&&file.exists())
				{
					dialog(getString(R.string.isDelete2));
				}
				else
				{
					EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, getString(R.string.noPicture), false);
				}
				return false;
			}
		});
	}
	
	/**
	 * 执行任务
	 */
	private void onExcute() {
		
		if(file!=null&&file.exists())
		{
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, getString(R.string.deletePicture)+file.getName(), false);
			file.delete();
			finish();
		}
	}
	
	/**
	 * 提示dialog
	 */
	private void dialog(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(
				PhotoDetailActivity.this);
		builder.setTitle(getString(R.string.prompt));
		builder.setMessage(message);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				onExcute();
				dialog.dismiss();
			}
		});
		builder.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		dialog = builder.create();
		dialog.show();
	}

	/**
	 * 根据选中图片加载内容
	 * @param file
	 */
	private void loadImage(File file)
	{
		//释放bitmap资源
		recycle();
		try {
			 
			BitmapFactory.Options options = new BitmapFactory.Options();  
			options.inJustDecodeBounds = false; // 设为 false
			bitmap = BitmapFactory.decodeFile(file.getPath(), options); 
			img_show.setImageBitmap(bitmap);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_BASE, getString(R.string.noPicture), false);
		}
	}
	
	/**
	 * 加载ACTIONBAR
	 */
	private void loadactionbar() {
		// TODO Auto-generated method stub
		actionBar.setCustomView(LayoutInflater.from(PhotoDetailActivity.this)
				.inflate(R.layout.actionbar_back_and_home, null));
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
		TextView title = (TextView) findViewById(R.id.txt_title);
		title.setText(R.string.PhotoAlbumActivity);
		ImageView back = (ImageView) findViewById(R.id.action_back);
		back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		ImageView home = (ImageView) findViewById(R.id.action_home);
		home.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(PhotoDetailActivity.this, MainActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
		});
	}

	/**
	 * 释放bitmap资源
	 */
	private void recycle(){
		
		if(bitmap!=null&&!bitmap.isRecycled())
		{
			bitmap.recycle();
			System.gc();
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		recycle();
	}
	
	
}
